// pages/apply/find/finddesc.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        catearr: [],
        teamarr: [],
        phone: '',
        is_team:1,
        team_id: '',
        is_cate1: 1,
        is_cate3:'',
        page: 0,
        keyword: '',
        cate_name: '',
        team_name: '',
        shufalse: false,
        is_all_show: 1,
        choosefalse:false,
        tablist:[{
                title:'全部',
                id:''
            },{
                title:'未报到',
                id:1
            },{
                title:'已报到',
                id:2
            },{
                title:'不来',
                id:3
            },{
                title:'已退费',
                id:4
            }
        ],
        is_report:'',
        arrlist:[],
        courselist:[],
        isCourse:false,
        course_id:'',
        courseName:'',
        discountlist:[],
        discount_id:'',
        discountName:'',
        phoneSearch:false
    },
    closech() {
        var that = this;
        that.setData({
            is_all_show: 1,
            is_team:1,
            is_cate:1,
            team_id:'',
            is_cate3:'',
            phone:'',
            choosefalse:false,
            cate_name:'',
            team_name:'',
            arrlist:[],
            isCourse: false,
            course_id:'',
            courseName:'',
            courselist:[],
            discountlist:[],
            discount_id:'',
            discountName:'',
            is_report:''
        })
        that.courses()
        that.getjnlist()
    },
    closeclcikll() {
        var that = this;
        that.setData({
            is_all_show: 1,
            isCourse: false,
            is_cate1: 1,
            is_team: 1,
        })

    },
    // 选择专业
    cateChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            is_cate3: item.id,
            cate_name:item.title,
            choosefalse:true,
            course_id:'',
            courseName:'',
            courselist:[],
            discountlist:[],
            discount_id:'',
            discountName:''
        })
        that.courses()
    },
    // 选择校区
    teamChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            team_id: item.id,
            team_name:item.title,
            choosefalse:true,
            course_id:'',
            courseName:'',
            courselist:[],
            discountlist:[],
            discount_id:'',
            discountName:''
        })
        that.courses()
    },
    // 选课程
    courseChange(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            course_id: item.id,
            courseName:item.title,
            choosefalse:true,
            discountlist:[],
            discount_id:'',
            discountName:''
        })
        that.getdiscount()

    },
    discountChange(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            discount_id: item.id,
            discountName:item.title,
            choosefalse:true
        })

    },
    cateclick() {
        var that = this;
        var is_cate1 = that.data.is_cate1;
        if (Number(is_cate1) == 1) {
            var is_cate1 = 2;
            var is_all_show = 2;
        } else {
            var is_cate1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_cate1: is_cate1,
            is_team: 1,
            is_all_show: is_all_show,
            isCourse: false,
        })
    },
    // /点击校区
    teamclick() {
        var that = this;
        var is_team = that.data.is_team;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: is_team1,
            is_all_show: is_all_show,
            is_cate1: 1,
            isCourse: false,
        })
    },
    searchclick(e) {
        var that = this;
        // keykown
        var keyword = e.detail.value.keykown;
        that.setData({
            phone: keyword,
            page: 0,
        })
            var tel = keyword.replace(/\s+/g, "");
            if ((/^1\d{10}$/.test(tel))) {
                that.listpeople();
            }else{
                
                // that.consStuList();
            }
    },
    messfocus(e) {
        this.setData({
            phone: e.detail.value
        })
        if(this.data.phone==''){
            this.setData({
                shufalse: false,
                phoneSearch:false,
                arrlist:[]
            })
        }
    },
    messcourse(e){
        this.setData({
            keyword: e.detail.value
        })
        this.courses()
    },
    sureclick() {
        var that = this;
        that.setData({
            is_all_show: 1,
            isCourse: false,
            is_cate1: 1,
            is_team: 1,
            shufalse:true,
            phoneSearch:false,
            page:0
        })
        that.getjnlist()
    },
    getdetail(e){
        console.log(e);
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        wx.setStorageSync('studentInfo', JSON.stringify(item));
        wx.navigateTo({
            url: '/pages/apply/skill/studentdesc',
        })
    },
    // 获取校区
    teams() {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: '',
                    title: '全部校区',
                }]
                that.setData({
                    teamarr: obj.concat(data)
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            consultant_id:app.globalData.admin_id
        })
        that.teams();
        that.jntsCate();
        that.courses()
        that.getjnlist()
    },
    jntsCate() {
        var that = this;
        share.wxRequest('crmapi/user/jntsCate', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                var obj = [{
                    id: '',
                    title: '全部分类',
                }]
                that.setData({
                  catearr: obj.concat(data)
                })
            }
        })
    },
    listpeople() {
        var that = this;
        wx.showLoading({
          title: '加载中~',
        })
        share.wxRequest('crmapi/user/findUserJn', {phone:that.data.phone,is_report:that.data.is_report}, function(res) {
            if (Number(res.data.errcode) == 0) {
                console.log(res.data.data);
                that.setData({
                    phoneSearch: true,
                    arrlist:res.data.data,
                    shufalse:false
                })
                console.log(that.data.arrlist);
            }
          wx.hideLoading();
         
        })
    },
    courses(){
        var that = this;
        let obj={
            area_id:that.data.team_id,
            pid:that.data.is_cate3,
            keyword:that.data.keyword
        }
        share.wxRequest('crmapi/user/courses', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                that.setData({
                    courselist:res.data.data,
                    discountlist:[]
                })
            }
        })
    },
    getCourse(){
        var that=this
        that.setData({
            isCourse: true,
            is_all_show:2
        })
    },
    getdiscount(){
        var that = this;
        let obj={
            course_id:that.data.course_id,
        }
        share.wxRequest('crmapi/user/discount', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                that.setData({
                    discountlist:res.data.data
                })
            }
        })
        
    },
    
    getjnlist(){
        var that = this;
        let obj={
            consultant_id:that.data.consultant_id,
            area_id:that.data.team_id,
            pid:that.data.is_cate3,
            course_id:that.data.course_id?that.data.course_id:'',
            discount_id:that.data.discount_id?that.data.discount_id:'',
            is_report:that.data.is_report?that.data.is_report:'',
            page:that.data.page,
            limit:10
        }
        share.wxRequest('crmapi/user/stuListJn', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data=res.data.data
                if (Number(that.data.page) > 0) {
                    var list = that.data.arrlist.concat(data);
                } else {
                    var list = data
                }
                that.setData({
                    phoneSearch: false,
                    arrlist:list,
                    shufalse:true
                })
                console.log(that.data.arrlist);
            }
        })
        

    },
    getReport(e){
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        console.log(item);
        this.setData({
            is_report:item.id
        })
        if(this.data.phone.length==11){
            this.listpeople();
        }else{
            this.setData({
                page:0
            })
            this.getjnlist()
        }
    },
        // crmapi/user/stuListJn
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var page = that.data.page;
        that.setData({
            page: Number(page) + 1
        })
    },
    onShow: function() {
        var that=this
        that.courses()
        that.getjnlist()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})